<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="训练项目" prop="trainObject">
        <el-select v-model="pram.trainObject" placeholder="训练项目" clearable style="width: 100%">
          <el-option
            v-for="(item,index) in categoryData.trainObjectList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="及格标准" prop="trainStandard" class="dislabel">-->
<!--        <el-input v-model="pram.trainStandard" maxlength="100" placeholder="及格标准"/>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="表现" prop="trainPerform" class="dislabel">-->
<!--        <el-input v-model="pram.trainPerform" type="textarea"  maxlength="250" :rows="3" placeholder="请输入表现" />-->
<!--      </el-form-item>-->
      <el-form-item label="分数" prop="trainScore">
        <el-input v-model.number="pram.trainScore" type="number" class="selWidth" placeholder="分数，至多一位小数" style="width: 100%"/>
      </el-form-item>
      <el-form-item label="评级" prop="trainLevel" class="dislabel">
        <el-select v-model="pram.trainLevel" placeholder="评级" clearable style="width: 100%">
          <el-option
            v-for="(item,index) in categoryData.trainLevelList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:person:train:update','admin:person:train:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as roleApi from '@/api/role.js'
import * as systemAdminApi from '@/api/systemadmin.js'
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    editKey: {
      type: Number,
      required: true,
      default: -1,
    },
    allCategoryData: {
      type: Object
    }
  },
  data() {
    /*const validatePhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请填写手机号'));
      } else if (!/^1[3456789]\d{9}$/.test(value)) {
        callback(new Error('手机号格式不正确!'));
      } else {
        callback();
      }
    };*/
    const validateTrainScore = (rule, value, callback) => {
      let match = /(^[0-9]\d{0,1}$)|(^[0-9]\d{0,1}(\.\d{1,1}?)$)/
      if (!value) {
        return callback(new Error('请填写分数'));
      } else if (value === 100){
        callback();
      } else if (!match.test(value)) {
        ///^[1-0]\d{0,3}(\.\d{1,2}?)$/.test(str)||
        callback(new Error('分数最多不超过100，小数点后面最多一位!'));
      } else {
        callback();
      }
    };
    return {
      constants:this.$constants,
      pram: {
        trainScore:null,
        trainObject: null,
        trainStandard: null,
        trainPerform:null,
        trainLevel:null,
        id: 0,

        trainObjectText:'',
        trainLevelText:'',
      },
      rules: {
        trainObject: [{ required: true, message: '请选择训练项目', trigger: ['blur', 'change'] }],
        //trainStandard: [{ required: true, message: '请填及格标准', trigger: ['blur', 'change'] }],
        //trainPerform: [{ required: true, message: '请填表现', trigger: ['blur', 'change'] }],
        trainScore: [{ required: true, validator:validateTrainScore, trigger: ['blur', 'change'] }],
        trainLevel: [{ required: true, message: '请选择评级', trigger: ['blur', 'change'] }],
      },
      categoryData:{
        trainObjectList:[],
        trainLevelList:[],
      },
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideInfoDialog')
    },
    initEditData() {
      this.categoryData = this.allCategoryData

      if (this.isCreate !== 1) return //编辑
      const { trainObject, trainStandard, trainPerform, trainLevel, trainScore, id} = JSON.parse(JSON.stringify(this.editData)) //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.trainObject = trainObject
      this.pram.trainStandard = trainStandard
      this.pram.trainLevel = trainLevel
      this.pram.trainPerform = trainPerform
      this.pram.trainScore = trainScore
      this.pram.id = id
    },
    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      const trainObjectList = this.categoryData.trainObjectList
      const trainLevelList = this.categoryData.trainLevelList
      trainObjectList.forEach((item)=>{
        if(item.id === this.pram.trainObject) this.pram.trainObjectText = item.name
      })
      trainLevelList.forEach((item)=>{
        if(item.id === this.pram.trainLevel) this.pram.trainLevelText = item.name
      })
      //systemAdminApi.adminAdd(this.pram).then(data => {
        //this.$message.success('创建军事训练综合评定成功')
        this.$emit('assignEditSaveAndHideEditDialog',this.pram)
      //})
    },
    handlerEdit() {
      const trainObjectList = this.categoryData.trainObjectList
      const trainLevelList = this.categoryData.trainLevelList
      trainObjectList.forEach((item)=>{
        if(item.id === this.pram.trainObject) this.pram.trainObjectText = item.name
      })
      trainLevelList.forEach((item)=>{
        if(item.id === this.pram.trainLevel) this.pram.trainLevelText = item.name
      })
      //systemAdminApi.adminAdd(this.pram).then(data => {
      //this.$message.success('创建军事训练综合评定成功')
      this.$emit('assignEditEditAndHideEditDialog',this.pram,this.editKey)
      //})
    },
  }
}
</script>

<style scoped>

</style>
